#
# Logging.py
#
# Author: Elysia Haight
#
from Observer import *

# This class logs moves made by players
class Logging(Observer) :
    
    #Constructor receives a stack of game moves as a parameter and opens dialog to receive file name
    def __init__(self, game, filename):
        game.register_observer(self)
        
        self.filename = filename
        self.game = game
        self.FILE = open( filename, "w" )
        
        # Write to file    
        for move in self.game.get_move_stack():
            self.write_move( move.to_string() )

    # Write a move line to the log file             
    def write_move(self, move):
        self.FILE.write( move )
        self.FILE.write( '\n' )
        self.FILE.flush()
            
    # Update the stack when notified, determine whether undo action has been called
    def update(self, subject, param ):

        if param == self.game.NOTIFY_MOVE :
            self.write_move( self.game.get_last_move().to_string() )
        elif param == self.game.NOTIFY_UNDO :
            self.write_move( "Undo " + self.game.get_last_move().to_string() )

    # close the file
    def close(self):
        self.FILE.close()

	
    #Record who has won the game 
    def write_win(self, winner): 
        if winner == 1 : 
            self.FILE.write( "Breaker has won the game" ) 
        elif winner == 2 : 
            self.FILE.write( "Maker has won the game" ) 
        self.FILE.flush()
